/**
 * User: hungdp
 * Date: 11/15/12
 * Time: 3:42 PM
 */
ebayitem = {}

ebayitem.pageIndex = 0;
ebayitem.pageSize = 100;

ebayitem.list = function(){
	main.init();
	ebayitem.pageIndex = 0;
	ebayitem.pageSize = 100;
    $("#content").html(fly.template('http://naima.vn/buyitem/main.html',{
	}));
    datepicker.init();
    ebayitem.load();
}

ebayitem.load = function(){
	fly.ajax({
		service: '/buyItem/list',
		data: {'pageIndex': ebayitem.pageIndex,'pageSize': ebayitem.pageSize},
		success: function(data){
			if(data.status){
				$('#tbl-list').html(fly.template('http://naima.vn/buyitem/list.html',{data: data.data.data}));
				$("#paging").html(fly.template('http://naima.vn/component/paging.html', {
                    data:data.data,
                    action:'ebayitem'
                }));
			}
			else
				popup.msg(data.message);
		}
	});
};

ebayitem.getEbay = function(id,eid){
	var rs;
	fly.ajax({
		service: '/buyItem/get',
		data: {id: eid},
		success: function(data){
			if(data.status){
				rs = data.data;
				ebayitem.updateEbay(rs,id);
			}
			else
				popup.msg(data.message);				
		}
	});
}

ebayitem.updateEbay = function(rs,id){
	popup.open('ebayitem','Mua hàng eBay', fly.template('http://naima.vn/buyitem/form.html',{data:rs}),[
		{
			title: 'Lưu lại',
			fn:function(){
				fly.submit({
					id: 'buyitem-form',
					service: '/buyItem/save',
					success: function(data){
						if(data.status){
							popup.msg('Lưu thành công');
							popup.close('ebayitem');
							var url = location.href.split('#')[1].split('/')[0];
							if(url == "order")
								order.load();
							else if(url == "ebayitem")
								ebayitem.load();
						}
						else
							popup.msg(data.message);
					}
				});
			}
		},
		{
			title: 'Hủy bỏ',
			fn:function(){
				popup.close('ebayitem');
			}
		}
	]);
	if(!order.data){
		fly.ajax({
			service: '/order/load',
			data: {
				'filter' : order.filter,
				'pageIndex' : order.pageIndex,
				'pageSize' : order.pageSize
			},
			success: function(data){
				if(data.status){
					order.data = data.data.data;
					ebayitem.appendDataForm();
				}
				else
					popup.msg(data.message);
			}
		});
	}
	else{
		ebayitem.appendDataForm(rs,id);
	}
	$('#buyitem-form .order').focus(function(){
		if($(this).val() == 'Nhập id hóa đơn'){
			$(this).val('');
		}
	});
	$('#buyitem-form .order').keyup(function(){
		var oid = $(this).val();
		if($(this).val() == 'Nhập id hóa đơn'){
			$(this).val('');
		}
		else{
			ebayitem.delay(function(){
				fly.ajax({
					service: '/order/get',
					data: {id: oid},
					success: function(data){
						if(data.status){
							$('._orderItemId').html('<option value="0">Chọn</option>');
							$.each(data.data, function(){
								$('._orderItemId').append('<option for="'+this.id+'" itemname="'+this.itemName+'" quantity="'+this.quantity+'" price="'+this.price+'" value="'+this.id+'">Bill'+this.id+'</option>');
							});
						}
						else
							popup.msg(data.message);
					}
				});
			}, 1000);
		}
	});
}

ebayitem.appendDataForm = function(rs,id){	
	$.each(order.data, function(){
		$.each(this['oitems'], function(){
			$('._orderItemId').append('<option for="'+this['orderitem'].id+'" itemname="'+this['orderitem'].itemName+'" quantity="'+this['orderitem'].quantity+'" price="'+this['orderitem'].price+'" value="'+this['orderitem'].id+'">Bill'+this['orderitem'].id+'</option>');
		})
	});
	$('#buyitem-form .itemname').val($('._orderItemId option[for='+id+']').attr('itemname'));
	$('#buyitem-form .quantity').val($('._orderItemId option[for='+id+']').attr('quantity'));
	$('#buyitem-form .itemprice').val(parseFloat($('._orderItemId option[for='+id+']').attr('price')));
	$('._orderItemId').change(function(){
		$('#buyitem-form .itemname').val($('._orderItemId option[for='+$(this).val()+']').attr('itemname'));
		$('#buyitem-form .quantity').val($('._orderItemId option[for='+$(this).val()+']').attr('quantity'));
		$('#buyitem-form .itemprice').val(parseFloat($('._orderItemId option[for='+$(this).val()+']').attr('price')));
	});
	$('._orderItemId').val(id);
	if(rs)
		$('#sl-status').val(rs.status);
}

ebayitem.delay = (function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    };
})();